package com.tensquare.base.service;

import cn.hutool.core.util.StrUtil;
import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import utils.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by WF on 2020/7/21 15:24
 */
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;
    //1.查询标签列表
    public List<Label> findAll() {
        return labelDao.findAll();
    }
    //2.添加标签
    public void add(Label label) {
        //2.1)设置标签的id
        label.setId(idWorker.nextId() + "");
        //2.2)保存到数据库中
        labelDao.save(label);
    }
    //3. 修改标签
    public void update(Label label) {
        labelDao.save(label);
    }
    //4.删除标签
    public void delete(String labelId) {
        labelDao.deleteById(labelId);
    }
    //5.根据id查询标签
    public Label findById(String labelId) {
        return labelDao.findById(labelId).get();
    }
    //6.条件查询带分页
    public PageResult<Label> search(Label label, int page, int size) {
        //6.1)根据查询条件创建规格对象
        Specification<Label> specification = createSpecification(label);
        //6.2)进行分页查询
        Page<Label> labelPage = labelDao.findAll(specification, PageRequest.of(page - 1, size));
        //6.3)返回pageResult对象
        return new PageResult<>(labelPage.getTotalElements(),labelPage.getContent());
    }
    //7.根据查询条件对象生成规格对象
    //HQL: hibernate Query Language
    //类似：select st.sname,st.sex,st.age,st.addr from Student st;
    private Specification<Label> createSpecification(Label label) {
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //1.定义代表查询条件的集合
                List<Predicate> predicateList = new ArrayList<>();
                //2.封装查询条件
                if(label != null){
                    if(StrUtil.isNotEmpty(label.getLabelname())){       //标签名称
                        predicateList.add(cb.like(root.get("labelname").as(String.class),"%" + label.getLabelname() + "%"));
                    }
                    if(StrUtil.isNotEmpty(label.getState())){
                        predicateList.add(cb.equal(root.get("state").as(String.class),label.getState()));
                    }
                    if(StrUtil.isNotEmpty(label.getRecommend())){
                        predicateList.add(cb.equal(root.get("recommend").as(String.class),label.getRecommend()));
                    }

                }
                //3.代表查询条件的数组
                Predicate[] predicates = new Predicate[predicateList.size()];
                //4.将条件集合转换为数组
                Predicate[] predicatesArr = predicateList.toArray(predicates);
                //5.将多个条件组装在一个Predicate对象中
                Predicate predicate = cb.and(predicatesArr);
                //6.返回
                return predicate;
            }
        };
    }
}
